<?php
/**
 * oppo产品模型
 *
 * @author qianyunlai.com
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_oppo_product extends discuz_table {

	public function __construct() {

		$this->_table = 'oppo_product';
		$this->_pk = 'product_id';

		parent::__construct();
	}

	/**
	 * 根据产品ID更新数据
	 *
	 * @param int $pid 产品ID
	 * @param array $data 新数据
	 * @return boolean
	 */
	public function update_by_pid($pid, $data) {
		if (($pid = intval($pid)) && !empty($data) && is_array($data)) {
			return DB::update($this->_table, $data, array('product_id' => $pid));
		}
		return 0;
	}

	/**
	 * 获取所有数据
	 *
	 * @param int $isformat 是否要格式化
	 */
	public function fetch_all($isformat) {
		$res = DB::fetch_all('SELECT * FROM %t ORDER BY product_id asc', array($this->_table));
		if ($isformat) {
			$list = array();
			foreach ($res as $row) {
				$list[$row['product_id']] = $row;
			}
			return $list;
		} else {
			return $res;
		}
	}

	/**
	 * 获取总记录数
	 */
	public function count() {
		return DB::result_first('SELECT COUNT(*) FROM %t', array($this->_table));
	}

	/**
	 * 删除数据
	 *
	 * @param int  $pid 产品ID
	 * @return boolean
	 */
	public function delete_by_pid($pid) {
		return dintval($pid, true) ? DB::delete($this->_table, DB::field('pid', $pid)) : 0;
	}
}

?>